package com.hospital.register.core.service.impl;

import com.hospital.register.cmn.client.DictFeignClient;
import com.hospital.register.constants.BaseEntityConstants;
import com.hospital.register.constants.DictCodeConstants;
import com.hospital.register.constants.HospitalConstants;
import com.hospital.register.core.repository.HospitalRepository;
import com.hospital.register.core.service.HospitalService;
import com.hospital.register.dto.core.common.HospitalPageDto;
import com.hospital.register.model.core.Hospital;
import com.hospital.register.vo.cmn.DictChildVo;
import com.hospital.register.vo.core.admin.HospitalPageVo;
import com.hospital.register.vo.core.common.HospitalQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    /**
     * 访问医院数据
     */
    @Autowired
    private HospitalRepository hospitalRepository;

    /**
     * 访问字典数据
     */
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public boolean insert(Hospital hospital) {
        Date now = new Date();
        hospital.setCreateTime(now);
        hospital.setUpdateTime(now);
        hospital.setStatus(HospitalConstants.STATUS_NOT_GO_LIVE);//未上线
        hospital.setIsDeleted(BaseEntityConstants.NOT_DELETED);//未删除
        return this.hospitalRepository.insert(hospital);
    }

    @Override
    public boolean updateByHoscode(Hospital hospital) {
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(BaseEntityConstants.NOT_DELETED);
        return this.hospitalRepository.updateByHosCode(hospital);
    }

    @Override
    public boolean existsByHoscode(String hoscode) {
        return this.hospitalRepository.existsByHoscode(hoscode);
    }

    @Override
    public HospitalQueryVo findByHoscode(String hoscode) {
        HospitalQueryVo vo = this.hospitalRepository.findByHoscode(hoscode);
        this.fillParam(vo);
        return vo;
    }

    @Override
    public Page<HospitalPageVo> findPage(HospitalPageDto dto) {
        Page<HospitalPageVo> page = this.hospitalRepository.findPage(dto);

        if (page.getContent().isEmpty()) {//结果为空
            return page;
        }

        Map<String, DictChildVo> typeMap = this.dictFeignClient.findValueMap(DictCodeConstants.HOSTYPE).getData();//医院类型字典
        Map<String, DictChildVo> provinceMap = this.dictFeignClient.findValueMap(DictCodeConstants.PROVINCE).getData();//字典：省

        for (HospitalPageVo vo : page.getContent()) {
            String typeName = typeMap.get(vo.getHostype()).getName();
            String provinceName = provinceMap.get(vo.getProvinceCode()).getName();
            String cityName = this.dictFeignClient.getName(vo.getCityCode());
            String districtName = this.dictFeignClient.getName(vo.getDistrictCode());

            vo.setParam(new HospitalPageVo.Param());
            vo.getParam().setHostypeString(typeName);
            vo.getParam().setFullAddress(provinceName + cityName + districtName);
        }

        return page;
    }

    @Override
    public boolean updateStatusById(String id, Byte status) {
        return this.hospitalRepository.updateStatusById(id, status);
    }

    @Override
    public HospitalQueryVo findById(String id) {
        HospitalQueryVo vo = this.hospitalRepository.findById(id);

        if (vo == null) {
            return null;
        }

        String typeName = this.dictFeignClient.getName(DictCodeConstants.HOSTYPE, vo.getHostype());//医院类型名称
        String provinceName = this.dictFeignClient.getName(vo.getProvinceCode());//所在省的名称
        String cityName = this.dictFeignClient.getName(vo.getCityCode());//所在市的名称
        String districtName = this.dictFeignClient.getName(vo.getDistrictCode());//所在区的名称

        vo.setParam(new HospitalQueryVo.Param());
        vo.getParam().setHostypeString(typeName);
        vo.getParam().setFullAddress(provinceName + cityName + districtName);

        return vo;
    }

    /**
     * 填充单个医院的额外参数（字典值）
     *
     * @param vo 要填充的对象
     */
    private void fillParam(HospitalQueryVo vo) {
        if (vo == null) {
            return;
        }

        String typeName = this.dictFeignClient.getName(DictCodeConstants.HOSTYPE, vo.getHostype());//医院类型名称
        String provinceName = this.dictFeignClient.getName(vo.getProvinceCode());//所在省的名称
        String cityName = this.dictFeignClient.getName(vo.getCityCode());//所在市的名称
        String districtName = this.dictFeignClient.getName(vo.getDistrictCode());//所在区的名称

        vo.setParam(new HospitalQueryVo.Param());
        vo.getParam().setHostypeString(typeName);
        vo.getParam().setFullAddress(provinceName + cityName + districtName);
    }


}
